﻿namespace YidanSoft.Library.EditorUtility.Collections
{
    using System;
    using System.Collections;
    using System.Reflection;
    using System.Threading;
    using System.Windows.Forms;
    using YidanSoft.Library.EditorUtility.CommandBars;

    public class ToolBarItemCollection : IEnumerable
    {
        private EventHandler Changed;
        private ArrayList items = new ArrayList();

        public event EventHandler Changed
        {
            add
            {
                EventHandler handler2;
                EventHandler changed = this.Changed;
                do
                {
                    handler2 = changed;
                    EventHandler handler3 = (EventHandler) Delegate.Combine(handler2, value);
                    changed = Interlocked.CompareExchange<EventHandler>(ref this.Changed, handler3, handler2);
                }
                while (changed != handler2);
            }
            remove
            {
                EventHandler handler2;
                EventHandler changed = this.Changed;
                do
                {
                    handler2 = changed;
                    EventHandler handler3 = (EventHandler) Delegate.Remove(handler2, value);
                    changed = Interlocked.CompareExchange<EventHandler>(ref this.Changed, handler3, handler2);
                }
                while (changed != handler2);
            }
        }

        public void Add(ToolBarItem item)
        {
            this.items.Add(item);
            this.RaiseChanged();
        }

        public void AddRange(ToolBarItem[] items)
        {
            foreach (ToolBarItem item in items)
            {
                this.items.Add(item);
            }
            this.RaiseChanged();
        }

        public void Clear()
        {
            while (this.Count > 0)
            {
                this.RemoveAt(0);
            }
        }

        public bool Contains(ToolBarItem item)
        {
            return this.items.Contains(item);
        }

        public IEnumerator GetEnumerator()
        {
            return this.items.GetEnumerator();
        }

        public int IndexOf(ToolBarItem item)
        {
            return this.items.IndexOf(item);
        }

        public void Insert(int index, ToolBarItem item)
        {
            this.items.Insert(index, item);
            this.RaiseChanged();
        }

        private void RaiseChanged()
        {
            if (this.Changed != null)
            {
                this.Changed(this, null);
            }
        }

        public void Remove(ToolBarItem item)
        {
            if (this.items.Contains(item))
            {
                this.items.Remove(item);
                this.RaiseChanged();
            }
        }

        public void RemoveAt(int index)
        {
            ToolBarItem item = (ToolBarItem) this.items[index];
            this.items.RemoveAt(index);
            this.RaiseChanged();
        }

        public int Count
        {
            get
            {
                return this.items.Count;
            }
        }

        internal ToolBarItem this[char mnemonic]
        {
            get
            {
                foreach (ToolBarItem item in this.items)
                {
                    string text = item.Text;
                    if ((text != string.Empty) && (text != null))
                    {
                        for (int i = 0; i < text.Length; i++)
                        {
                            if ((((text[i] == '&') && ((i + 1) < text.Length)) && (text[i + 1] != '&')) && (mnemonic == char.ToUpper(text[i + 1])))
                            {
                                return item;
                            }
                        }
                    }
                }
                return null;
            }
        }

        internal ToolBarItem this[Keys shortcut]
        {
            get
            {
                foreach (ToolBarItem item in this.items)
                {
                    if (((item.Shortcut == shortcut) && item.Enabled) && item.Visible)
                    {
                        return item;
                    }
                }
                return null;
            }
        }

        public ToolBarItem this[int index]
        {
            get
            {
                return (ToolBarItem) this.items[index];
            }
        }
    }
}

